﻿using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using UnityEngine.UI;
using System;
using UnityEngine.SceneManagement;

public class LoadAssetBundle : MonoBehaviour
{
    private string basePath;

    //public Text message;

    //public GameObject loadModelData;

    //public Animator loading;
    void Start()
    {


#if UNITY_EDITOR
        basePath = "file://" + Application.streamingAssetsPath;
#elif UNITY_ANDROID
            //basePath="file://"+Application.persistentDataPath+"/ResourcesAssetBundle/";
        basePath="jar:file://" + Application.dataPath + "!/assets";
#endif
        //assetbundle 全名
        StartCoroutine(LoadFromAssetBundle("capsule"));
        StartCoroutine(LoadFromAssetBundle("cube"));
        StartCoroutine(LoadFromAssetBundle("cylinder"));
    }
    /// <summary>
    /// 资源加载
    /// </summary>
    /// assetBundleList用于卸载
    /// <returns></returns>
    IEnumerator LoadFromAssetBundle(string filename)
    {
        List<AssetBundle> assetBundleList = new List<AssetBundle>();  
        WWW wwwPrefab = new WWW(@basePath + "/" + filename);
        Debug.Log(@basePath + "/" + filename);
        yield return wwwPrefab;

        while (!wwwPrefab.isDone)
        {
            //message.text = message.text + "wwwPrefab return null";
            yield return null;
        }

        if (!string.IsNullOrEmpty(wwwPrefab.error))
        {
            //message.text = message.text + "wwwPrefab error break" + wwwPrefab.error;
            Debug.Log(wwwPrefab.error);
            yield break;
        }
        else
        {
            AssetBundle abPrefab = wwwPrefab.assetBundle;

            assetBundleList.Add(abPrefab);

            //Debug.Log();
            //assetbundle 名
            Instantiate(abPrefab.LoadAsset<GameObject>(filename));
             
        }
         
        foreach (var abl in assetBundleList)
        {
            if (abl != null)
            {
                abl.Unload(false);
            }
        }

    }

}
